package com.atguigu.tms.mock.task;


import com.atguigu.mock.util.MyDateUtil;
import com.atguigu.tms.mock.bean.OrderCargo;
import com.atguigu.tms.mock.config.AppConfig;
import com.atguigu.tms.mock.service.AReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;

@Component
@Slf4j
@Transactional(propagation = Propagation.NEVER)
public class ChronoTrigger {



    @Autowired
    ExpressCollectMockTask expressCollectMockTask;

    @Autowired
    SortingMockTask sortingMockTask;

    @Autowired
    TransportMockTask transportMockTask;

    @Autowired
    ExpressDeliveryTask expressDeliveryTask;

    @Autowired
    SignOrderTask signOrderTask;

    @Autowired
    OrderMockTask orderMockTask;

    @Autowired
    PaymentMockTask paymentMockTask;

    @Autowired
    AReportService aReportService;

    public void  runTime() {


        for (int hour = 0; hour < 24; hour++) {

            AppConfig.cur_hour_clock=hour;
            log.warn("时间："+hour+":00");
            for (int minute = 0; minute < 60; minute++) {
                Date date = MyDateUtil.initDate(AppConfig.mock_date_str, hour, minute);
                runEveryMinute(date,hour,minute);

            }

        }

    }


    private void runEveryMinute(Date curDate,int curHour,int curMinute){

        // 下单   7--22点  每6分钟
        if(curHour>7 && curHour<22 && curMinute%6==0){
            orderMockTask.doOrder(curDate,curHour,curMinute);
        }
        //支付 每8分钟
        if(curHour>7 && curHour<22 && curMinute%8==0){
            paymentMockTask.doPayment(curDate );

        }

        // 收揽快递
        Integer[] collect_start_clock={9,15};

        if( curHour>=9 && curHour<19  && curMinute%10==0) {
            Integer collectStartClock=  curHour>collect_start_clock[1]?collect_start_clock[1]: collect_start_clock[0];
            expressCollectMockTask.doCollect(  curDate,collectStartClock,curHour);
        }

        // 提交快递
        Integer[] commit_clock={11,17};
        if( (ArrayUtils.contains(  commit_clock,curHour))  && curMinute==30) {
             //检查揽收任务但是未提交的
            //提交任务
            //进站 order_bound
            // 生成路线规划
            expressCollectMockTask.doCommit(curHour);
        }
        //提前班次前一小时中分拣
        Integer[] shift_clock=AppConfig.shift_clock;
        if( (ArrayUtils.contains(  shift_clock,curHour+2))  && curMinute==50) {
            // 检查 order_bound   所有有in 无out的
            // 检查下一站物流方向
            sortingMockTask.doTask(curDate,curHour,curHour+2);


        }
        if( (ArrayUtils.contains(  shift_clock,curHour+1)) && curMinute%5==0){
            // 分拣员 生成运货任务
            //       路线规划完成 生成派送任务
            sortingMockTask.doSort(  curDate,5  );
        }

        //发车
        if( (ArrayUtils.contains(  shift_clock,curHour ))  && curMinute==0) {
            //检查 待发车任务
            // 启动 发车任务
            transportMockTask.doTransportTaskStart(curDate,curHour);

        }
        //开始派货
        Integer[] send_clock={9,13};
        if( (ArrayUtils.contains(  send_clock,curHour ))  && curMinute==0) {
            expressDeliveryTask.startDelivery(curDate);
        }
        //完成派货
        if(curHour>=9 && curHour<19  && curMinute ==50){
            expressDeliveryTask.doDelivery(curDate);
        }
        //签收
        if(curHour>=10 && curHour<=20 && curMinute ==0){
            signOrderTask.doSign(curDate);
        }

        //更新行车进度  每5分钟
        if(  curMinute%5==0) {
            //更新任务进度
            // 检查已出发的运输任务 根据 消耗时长获得行驶里程 更新进度
            // 如果已完成里程超过总里程则 更新完成
            transportMockTask.doDrive(curDate,  curHour ,curMinute,5);

        }
        if(curHour==23&&curMinute==59){
            aReportService.report();
        }

    }




}
